package com.huluobo.appbase.ui

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.TextUtils
import android.view.KeyEvent
import android.view.View
import android.widget.LinearLayout
import android.widget.ProgressBar
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import com.huluobo.appbase.R
import com.huluobo.appbase.utils.DeviceUtil

/**
 * Created by lc on 2023/3/2.
 *  private constructor(mContext: Context)私有化构造函数,外部无法直接new
 */
class LoadingDialog private constructor(mContext: Context) : Dialog(mContext), LifecycleObserver {
    private lateinit var tv_bottom_desc: TextView
    private lateinit var ll_dialog: LinearLayout
    private lateinit var progressBar: ProgressBar

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.view_dialog_loading)
        tv_bottom_desc = findViewById(R.id.tv_bottom_desc)
        ll_dialog = findViewById(R.id.ll_dialog)
        progressBar = findViewById(R.id.progressBar)

        val dialogWindow = window
        dialogWindow?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    }

    /**
     *  是否可以触摸取消,默认值为false
     */
    private var cancelOnTouchOutsideValue = false

    /**
     * 是否可以点击返回键取消,默认值为false
     */
    private var canceledOnBack = false

    /**
     * 背景框默认设置
     */
    private var dialogBackgroundResource = R.drawable.drawable_loading_toast

    /**
     * 底部文字的颜色
     */
    private var bottomTextColor = mContext.resources.getColor(R.color.white)

    /**
     * 底部文字大小
     */
    private var bottomTextSize = 15f

    /**
     * 弹窗布局的大小 宽度
     */
    private var dialogWidth = 0f

    /**
     * 弹窗布局的大小 高度
     */
    private var dialogHeight = 0f

    /**
     * progressBar大小
     */
    private var progressBarSize = 0f

    /**
     * 底部文字的描述
     */
    private var bottomDesc: String? = null

    override fun show() {
        if (ownerActivity != null && !ownerActivity!!.isFinishing) {
            super.show()
            show(this)
        }
    }

    private fun show(loadingDialog: LoadingDialog) {
        if (!TextUtils.isEmpty(bottomDesc)) {
            tv_bottom_desc.text = bottomDesc
            tv_bottom_desc.visibility = View.VISIBLE
        }

        //屏蔽返回按键
        if (!canceledOnBack) {
            setOnKeyListener { dialog, keyCode, event ->
                keyCode == KeyEvent.KEYCODE_BACK
            }
        }

        //设置触摸外部取消dialog
        setCanceledOnTouchOutside(cancelOnTouchOutsideValue)

        //设置背景
        ll_dialog.setBackgroundResource(dialogBackgroundResource)

        //设置字体颜色
        tv_bottom_desc.setTextColor(bottomTextColor)

        //设置字体大小
        tv_bottom_desc.textSize = bottomTextSize

        //设置dialog的宽高
        if (dialogWidth != 0f) {
            val layoutParams = ll_dialog.layoutParams
            layoutParams.width = DeviceUtil.dp2px(context, dialogWidth)
            layoutParams.height = DeviceUtil.dp2px(context, dialogHeight)
            ll_dialog.layoutParams = layoutParams
        }

        //设置progressBar的宽高
        if (progressBarSize != 0f) {
            val layoutParams = ll_dialog.layoutParams
            layoutParams.width = DeviceUtil.dp2px(context, progressBarSize)
            layoutParams.height = DeviceUtil.dp2px(context, progressBarSize)
            progressBar.layoutParams = layoutParams
        }
    }

    /**
     * 使用构建者模式创建Dialog,重点:返回的都是对象本身
     */
    class Builder(context: Context) {
        private var loadingDialog: LoadingDialog = LoadingDialog(context)

        /**
         * 设置底部文字描述
         */
        fun setBottomDesc(bottomDesc: String): Builder {
            loadingDialog.bottomDesc = bottomDesc
            return this
        }

        fun setCanceledOnTouchOutsideValue(canceled: Boolean): Builder {
            loadingDialog.cancelOnTouchOutsideValue = canceled
            return this
        }

        fun setCanceledOnBackValue(onBack: Boolean): Builder {
            loadingDialog.canceledOnBack = onBack
            return this
        }

        fun setDialogBackground(resource: Int): Builder {
            loadingDialog.dialogBackgroundResource = resource
            return this
        }

        fun setBottomTextColor(resource: Int): Builder {
            loadingDialog.bottomTextColor = resource
            return this
        }

        fun setBottomTextSize(size: Float): Builder {
            loadingDialog.bottomTextSize = size
            return this
        }

        fun setDialogSize(dialogWidth: Float, dialogHeight: Float): Builder {
            loadingDialog.dialogWidth = dialogWidth
            loadingDialog.dialogHeight = dialogHeight
            return this
        }

        fun setProgressBarSize(size: Float): Builder {
            loadingDialog.progressBarSize = size
            return this
        }

        fun create(): LoadingDialog {
            return loadingDialog
        }
    }

    /**
     * 使用LifeCycle自动销毁弹窗
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    private fun onDestroy() {
        if (isShowing) {
            dismiss()
        }
    }

    init {
        if (mContext is Activity) {
            setOwnerActivity(mContext)
        }

        if (mContext is ComponentActivity) {
            mContext.lifecycle.addObserver(this)
        }
    }
}